const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')
Page(Monitor.hookApp({

  data: {
    waysArr:[],
    qingdan: `${getApp().globalData.imgUrl}jsy-newshop清单.svg`,
    beihuo: `${getApp().globalData.imgUrl}jsy-newshop备货复制.svg`,
    imgUrls: getApp().globalData.imgUrls
  },
  onLoad(o){
    console.log(o.order_id, o.distributiontype)
    this.setData({
      order_id: o.order_id,
      distributiontype: o.distributiontype
    })
  },
  onShow(){
    
    this.getInfo()
  },
  copyText(e) { //点击复制
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  specifications(trg) { //商品规格转换
    let len = trg.length
    for (let i = 0; i < len; i++) {
      let size = ''
      let str = `${trg[i].sku_type}`
      trg[i].sku_type = JSON.parse(str)
      for (let j = 0; j < trg[i].sku_type.length; j++) {
        for (let prop in trg[i].sku_type[j]) {
          size += trg[i].sku_type[j][prop] + " / "
        }
      }
      trg[i].size = size
      trg[i].size = trg[i].size.substring(0, trg[i].size.length - 3)
    }
    return trg
  },
  getInfo(){
    util.API('pyapi/oder_if', {
      order_id: this.data.order_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let { code, data, orders_goods_list, distribution_name} = res
      if(code == 200){
        let date = util.getTimeDian(data.ctime)
        console.log(date)
        data.ctime = `${date.y}.${date.m}.${date.d} ${date.h}:${date.i}:${date.s}`
        orders_goods_list = this.specifications(orders_goods_list)
        let waysArr = this.setWaysArr([], this.data.distributiontype, JSON.stringify(distribution_name))
        this.setData({
          waysArr,
          data,
          orders_goods_list
        })
      }
    })
  },
  setWaysArr(waysArr, distribution_type, distribution_name) {  // 设置自定义配送信息
    distribution_type = distribution_type.split(',');
    distribution_name = JSON.parse(distribution_name)
    let len = distribution_type.length
    for (let i = 0; i < len; i++) {
      waysArr.push(distribution_name[distribution_type[i]])
    }
    return waysArr
  },
  cancellationOfOrder(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: e.currentTarget.dataset.status == 'shanchu' ? '是否确定删除订单' : '是否确定取消订单',
      success(res) {
        if (res.confirm) {
          util.API('pyapi/Cancellation_order', {
            order_id: e.currentTarget.dataset.id
          }, 'application/x-www-form-urlencoded').then(res => {
            if(res.code == 200){
              wx.navigateTo({
                url: `../DingDanXiangQingQuXiao/DingDanXiangQingQuXiao?order_id=${e.currentTarget.dataset.id}&distributiontype=${e.currentTarget.dataset.distributiontype}`,
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
}))